package YuanFuDao2;

/**
 * 二分查找
 */
public class Main33 {
    public static void main(String[] args) {
        int[] nums = {2,5,7,9,12,14,20,26,30};
        System.out.println(binarySearch(nums,26));
    }

    public static int binarySearch(int[] nums,int target){
        int left = 0;
        int right = nums.length-1;
        int mid;
        while(left <= right){

            mid = left + (right-left)/2;
            if(nums[mid] == target){
                return mid;
            }else if(nums[mid] < target){
                left = mid+1;
            }else{
                right = mid-1;
            }
        }
        return  -1;
    }

}
